/***********************************************************************************************************************
* Copyright (C) . All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : main.c
* Version      :
* Device(s)    : BAT32G137
* Tool-Chain   : MDK(armcc)
* Description  : This file is a template.
* Creation Date: 2019/4/30
***********************************************************************************************************************/

/***********************************************************************************************************************
Macro Definitions
***********************************************************************************************************************/

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include <stdio.h>
#include "BAT32G137.h"
#include "flash.h"
#include "sci.h"
#include "pcbz.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
uint32_t g_ticks;
uint8_t ram_opt[4] = {};

void delayMS(uint32_t n)
{
    g_ticks = n;

    while(g_ticks);
}

int main()
{
	MD_STATUS status;
    uint32_t msCnt; 	// count value of 1ms
    uint8_t bten;
    uint32_t opbyte;
    uint8_t i;

//-----------------------------------------------------------------------
// Systick setting
//-----------------------------------------------------------------------
    g_ticks = 1000; 	// 1000ms
    SystemCoreClockUpdate();
    msCnt = SystemCoreClock / 1000;
    SysTick_Config(msCnt);
    
	  //delayMS(2500);
	
//-----------------------------------------------------------------------
// PCLBUZ0 output 
//----------------------------------------------------------------------- 
//	PCBZ_Init(PCBZ_CHANNEL_0, PCBZ_FMAIN_3);
	
//-----------------------------------------------------------------------
// LED blinky on target board
// P71 and P72 drives LED on EVB
//-----------------------------------------------------------------------
    PORT->P7    = 0x04U;
    PORT->PM7  &= ~(7 << 1); // P71/LED, P72/LED, P73/LED for EVB & BAT_sub
    //PORT->PM7  &= ~(3 << 1); // P71/LED, P72/LED

//-----------------------------------------------------------------------
// Init UART0 for retarget printf/scanf etc. 
//----------------------------------------------------------------------- 
#if 1
	SystemCoreClockUpdate();
	status = UART0_Init(SystemCoreClock, 19200);
	if(status == MD_ERROR)
	{
		while(1);
	}
  
  /* RAM Parity Error Reset Disable */
  SAF->RPECTL |= 0x80U;	
	
		  //delayMS(2500);
	
	printf("\nHello, UART\n");
  bten = *((uint8_t *)0x500005);
	printf("BTEN = %02X\n", bten);
  opbyte = *((uint32_t *)0x000000C0);
	printf("OPBYTE = %08X\n", opbyte);
	printf("RESF = %02X\n", RST->RESF);
	printf("RPECTL = %02X\n", SAF->RPECTL);
		
#endif
//-----------------------------------------------------------------------
// Boot swap copy
//-----------------------------------------------------------------------
    flash_write(0x00001000, 0x100, (uint8_t *) 0x00);

		// chang option byte
    ram_opt[0] = opbyte;
    ram_opt[1] = opbyte >> 8;
    ram_opt[2] = opbyte >> 16;
    if (bten & 0x01)
    {
        ram_opt[2] = 0xEB;
    }
    else
    {
        ram_opt[2] = 0xF8;
    }
    ram_opt[3] = opbyte >> 24;
    flash_write(0x000010C0, 0x4, (uint8_t *) &ram_opt[0]);

		// boot swap
    if (bten & 0x01)
    {
        ram_opt[0] = 0xFE;
        flash_write(0x500005, 0x1, (uint8_t *) &ram_opt[0]);
    }
    else
    {
        ram_opt[0] = 0xFF;
        flash_write(0x500005, 0x1, (uint8_t *) &ram_opt[0]);
    }


    //delayMS(2500);

    for(i = 0; i < 20; i++)
    {
        delayMS(250);
        PORT->P7 ^= (1 << 1); 	// Toggle P71
        PORT->P7 ^= (1 << 2); 	// Toggle P72
        PORT->P7 ^= (1 << 3); 	// Toggle P73
    }

//    delayMS(2500);
    NVIC_SystemReset();

    while(1);
}

/***********************************************************************************************************************
* Function Name: SysTick Handler
* Description  : Decreament the g_ticks value
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void SysTick_Handler(void)
{
    g_ticks--;
    WDT->WDTE = 0xACU;     /* restart watchdog timer */
}
